package com.gang.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

/**同步
 * @author maj
 * @ClassName JmsConfig
 * @Description
 * @date 2020-06-19 10:45
 */
@Configuration
@EnableJms
public class ActiveMqConfigsyn {

  @Value("${topic.name}")
  private String topicName;

  @Value("${spring.activemq.user}")
  private String usrName;

  @Value("${spring.activemq.password}")
  private  String password;

  @Value("${spring.activemq.broker-url}")
  private  String brokerUrl;

  @Bean
  public ActiveMQConnectionFactory connectionFactory() {
    ActiveMQConnectionFactory activeMQConnectionFactory =
        new ActiveMQConnectionFactory(usrName,
        password, brokerUrl);
//    activeMQConnectionFactory.setDispatchAsync(true);
    return activeMQConnectionFactory;
  }

 @Bean(name="jmsListenerContainerQueue")
  public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
    DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
    //设置为发布订阅方式, 默认情况下使用的生产消费者方式
    defaultJmsListenerContainerFactory.setPubSubDomain(false);
    defaultJmsListenerContainerFactory.setConnectionFactory(connectionFactory);
    return defaultJmsListenerContainerFactory;
  }

 @Bean(name="jmsListenerContainerTopic")
  public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
    DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
    //设置为发布订阅方式
    defaultJmsListenerContainerFactory.setPubSubDomain(true);
    defaultJmsListenerContainerFactory.setConnectionFactory(connectionFactory);
    return defaultJmsListenerContainerFactory;
  }

}
